package edu.hrbu.trade01.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.hrbu.trade01.api.MenuApi;
import edu.hrbu.trade01.entity.Menu;
import edu.hrbu.trade01.entity.RoleMenu;
import edu.hrbu.trade01.entity.User;
import edu.hrbu.trade01.entity.UserRole;
import edu.hrbu.trade01.service.MenuService;
import edu.hrbu.trade01.service.RoleMenuService;
import edu.hrbu.trade01.service.UserRoleService;
import edu.hrbu.trade01.service.UserService;
import edu.hrbu.trade01.util.MenuUtil;
import edu.hrbu.trade01.util.R;
import edu.hrbu.trade01.vo.UserLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static edu.hrbu.trade01.util.ConstUtil.LOGIN_SESSION;

/**
 * @author 徐登宇
 */
@RestController
@RequestMapping("/home")
@Slf4j
public class HomeController {

    private LineCaptcha lineCaptcha;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    // 生成验证码
    @GetMapping("/createCode")
    public R createCode(HttpServletResponse response) {
        //HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
        lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 10);
        log.info("【验证码】：" + lineCaptcha.getCode());
//        response.setContentType("image/jpeg");
//        response.setHeader("Pragma", "No-cache");
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    // 校验验证码
    @GetMapping("/verifyCode")
    public R verifyCode(@RequestParam("code") String code) {
        if (lineCaptcha.getCode().equals(code)) {
            return R.ok();
        }
        return R.fail();
    }

    // 登录
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo userLoginVo,
                   HttpSession session) {
        User user =  userService.login(userLoginVo);
        if (user==null) return R.fail("登录失败");
        // 将登录的用户存到session中
        session.setAttribute(LOGIN_SESSION, user);
        // 获取对应的菜单
//        log.info("[[wdweqw");
        return R.ok(user);
    }

    // 登出
    @GetMapping("/logout")
    public R logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().removeAttribute(LOGIN_SESSION);
        response.sendRedirect(request.getContextPath()+"/web/login.html");
        return R.ok();
    }

    // 通过用户获取对应的菜单项
    @RequestMapping("/menu")
    public Object menu(@RequestParam("userId") Long userId) {

        MenuApi menuApi = menuService.getMenuTreeByUserId(userId);

        return R.ok(menuApi).getData();
    }

    // 修改密码
    @PostMapping("/updatePwd")
    public R updatePwd(HttpServletRequest request,@RequestBody User user) {
        userService.updateById(user);
        request.getSession().removeAttribute(LOGIN_SESSION);
        return R.ok();
    }

}
